<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/3/9
  Time: 10:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@include file="../../../base.jsp" %>
<div class="modal-header">
    <h3 class="text-left text-primary">
        功能配置
    </h3>
</div>
<br>
<div class="">
    <form id="sysConfigForm">
        <table class="table table-hover">
            <thead>
            <tr>
                <th>#</th>
                <th>code</th>
                <th>名称</th>
                <th>值</th>
                <th>说明</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <c:forEach items="${sysConfigList}" var="sysConfig" varStatus="vs">
                <tr>
                    <th>
                            ${vs.count}
                    </th>
                    <td>
                            ${sysConfig.code}
                    </td>
                    <td>
                            ${sysConfig.name}
                    </td>
                    <td class="edit ${sysConfig.configId}" name='sysConfig.value' span="" type="text">
                            ${sysConfig.value}
                    </td>
                    <td>
                            ${sysConfig.description}
                    </td>
                    <td>
                        <button type="button" class="btn btn-primary editBtn edit" configId='${sysConfig.configId}'
                                onclick="sysConfig.toEdit('${sysConfig.configId}',this)">修改
                        </button>
                        <button type="button" class="btn btn-danger saveBtn hidden edit submit"
                                configId='${sysConfig.configId}'
                                onclick="sysConfig.save('${sysConfig.configId}')">提交
                        </button>
                        <button type="button" class="btn btn-primary saveBtn hidden cancel edit"
                                configId='${sysConfig.configId}'
                                onclick="sysConfig.cancel('${sysConfig.configId}')">取消
                        </button>
                    </td>
                </tr>
            </c:forEach>
            </tbody>
        </table>
    </form>
</div>

<script>
    var sysConfig = {
        cancel: function (configId) {

            var elms = $("[editInput=1]");
            $(elms).each(function (i, o) {
                var old = $(o).attr("old");
                var td = $(o).parent();
                td.html(old + td.attr("span"));
            });

            $(".edit").removeClass("hidden");
            $(".submit").addClass("hidden");
            $(".cancel").addClass("hidden");
            $("button.edit").removeAttr("disabled")
        },
        toEdit: function (configId, elem) {
            $('.edit.' + configId).each(function () {
                var old = $(this).html();
                var name = $(this).attr("name");
                var span = $(this).attr("span");
                old = old.replace(span, "");
                var type = $(this).attr("type")
                var html = "<div class=\"input-group\" editInput='1' old='" + old + "'>\n" +
                    "    <input name='" + name + "' type=\"" + type + "\" class=\"form-control\"  aria-describedby=\"basic-addon2\" value=" + old + ">\n" +
                    "    <span class=\"input-group-addon\" id=\"basic-addon2\">" + span + "</span>\n" +
                    "</div>"
                $(this).html(html);

            });


            $("button.edit").attr("disabled", "");
            $("[configId=" + configId + "]").removeClass("hidden").removeAttr("disabled");
            $(".cancel").removeAttr("disabled");
            $(elem).addClass("hidden");
        },
        save: function (configId) {
            debugger
            var form = $("#sysConfigForm");
            var data = JWB.utils.fromSerialize(form);
            data["sysConfig.configId"] = configId;
            var url = base.basePath + "sysconfig/save";
            $.ajax(
                {
                    url: url,
                    data: data,
                    type: "POST",
                    success: function (res) {
                        alert(res.msg);
                        if (res.code == 0) {
                            index_js.reloadRightPage();
                        }
                    },
                    error: function (jXHR, textStatus, errorThrown) {
                        JWB.http.errorHandle(jXHR, textStatus, errorThrown);
                    }
                }
            );
        }
    }
</script>